package com.wrench.elevator.tida.util;

import com.alibaba.fastjson.JSON;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SendMessage
{
    public static void main(String[] args)
        throws IOException
    {
        send("18668047297");
    }
    
    public static String send(String tel)
        throws IOException
    {
        // 请求的webservice的url
        URL url = new URL("http://121.196.208.240/smsJson.aspx");
        // 创建http链接
        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        
        // 设置请求的方法类型
        httpURLConnection.setRequestMethod("POST");
        
        // 设置请求的内容类型
        httpURLConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
        
        // 设置发送数据
        httpURLConnection.setDoOutput(true);
        // 设置接受数据
        httpURLConnection.setDoInput(true);
        
        // 发送数据,使用输出流
        OutputStream outputStream = httpURLConnection.getOutputStream();
        String verifyCode = getRandomVerifyCode();
        StringBuilder sb = new StringBuilder();
        sb.append("userid=sd&account=bswlhy&password=")
            .append(encode("bswl001"))
            .append("&mobile=")
            .append(tel)
            .append("&content=您的验证码为：")
            .append(verifyCode)
            .append("【电梯公交后台】")
            .append("&sendTime=&action=send&extno=");
        
        // 发送数据
        outputStream.write(sb.toString().getBytes());
        
        // 接收数据
        InputStream inputStream = httpURLConnection.getInputStream();
        
        // 定义字节数组
        byte[] b = new byte[1024];
        
        // 定义一个输出流存储接收到的数据
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        
        // 开始接收数据
        int len = 0;
        while (true)
        {
            len = inputStream.read(b);
            if (len == -1)
            {
                // 数据读完
                break;
            }
            byteArrayOutputStream.write(b, 0, len);
        }
        
        // 从输出流中获取读取到数据(服务端返回的)
        String response = byteArrayOutputStream.toString();
        
        String returnstatus = JSON.parseObject(response).getString("returnstatus");
        if ("Success".equals(returnstatus))
        {
            return verifyCode;
        }
        else
            return null;
    }
    
    public static String getRandomVerifyCode()
    {
        return String.valueOf((int)((Math.random() * 9 + 1) * 100000));
    }
    
    // md5 32位大写加密
    public static String encode(String text)
    {
        try
        {
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(text.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result)
            {
                int number = b & 0xff;
                String hex = Integer.toHexString(number);
                if (hex.length() == 1)
                {
                    sb.append("0" + hex);
                }
                else
                {
                    sb.append(hex);
                }
            }
            return sb.toString().toUpperCase();
        }
        catch (NoSuchAlgorithmException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return "";
    }
}
